using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using System.IO;
using Microsoft.Win32;
using Microsoft.Practices.ComponentModel;
using System.ComponentModel.Design;
using Microsoft.Practices.Common.Services;
using System.Web.Configuration;
using EnvDTE;
using Config = System.Configuration.Configuration;
using Microsoft.Practices.RepositoryFactory.Actions.Configuration.Helpers;
using Microsoft.Practices.RecipeFramework.Library;

namespace Microsoft.Practices.RepositoryFactory.Actions.Configuration
{
    /// <summary>
    /// Action that gets a configuration object
    /// </summary>
    [ServiceDependency(typeof(ITypeResolutionService))]
    public class GetConfigurationAction : ConfigurableAction
    {
        #region Input Properties

        Project project;
        /// <summary>
        /// Gets or sets the project.
        /// </summary>
        /// <value>The project.</value>
        [Input(Required = true)]
        public Project Project
        {
            get { return project; }
            set { project = value; }
        }

        #endregion

        #region Output Properties

        Config configuration;
        /// <summary>
        /// Gets or sets the configuration.
        /// </summary>
        /// <value>The configuration.</value>
        [Output]
        public Config Configuration
        {
            get { return configuration; }
            set { configuration = value; }
        }

        /// <summary>
        /// Gets the configuration file path.
        /// </summary>
        /// <value>The configuration file path.</value>
        [Output]
        public string ConfigurationFilePath
        {
            get { return configuration.FilePath; }
        }

        #endregion

        #region Action Implementation

        private bool configAdded;

        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Execute"/>.
        /// </summary>
        public override void Execute()
        {
            this.Configuration = ConfigurationVSHelper.GetConfiguration(project);
            
            if (this.Configuration == null)
            {
                //Add a new configuration
                this.Configuration = ConfigurationVSHelper.AddNewConfiguration(project, base.Container);
                configAdded = true;
            }
        }

        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Undo"/>.
        /// </summary>
        public override void Undo()
        {
            if (configAdded)
            {
                ProjectItem item = ConfigurationVSHelper.GetConfigurationItem(this.project);
                if (item != null)
                {
                    item.Delete();
                }
            }
            this.Configuration = null;
        }

        #endregion
    }
}
